﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OfficeOpenXml;

namespace DVDFilmoteka.src.Exporters
{
    public class ExcelDatabaseExporter : IDatabaseExporter
    {
        private List<MovieStorage.MovieDataStore12> database = null;
        private MovieStorage.MovieDataStore12 columns = null;
        private String fileName = null;

        #region IDatabaseExporter Members

        public List<MovieStorage.MovieDataStore12> Database
        {
            get
            {
                return database;
            }
            set
            {
                database = value;
            }
        }

        public MovieStorage.MovieDataStore12 ColumnsToExport
        {
            get
            {
                return columns;
            }
            set
            {
                columns = value;
            }
        }

        public String FileName
        {
            get
            {
                return fileName;
            }
            set
            {
                fileName = value;
            }
        }

        public void Export()
        {
            if (database == null || columns == null || String.IsNullOrEmpty(fileName)) return;

            ExcelPackage xlPackage = new ExcelPackage(new FileInfo(fileName));
            xlPackage.Workbook.Properties.Title = "DVDFilmotéka Excel export";
            xlPackage.Workbook.Properties.Author = "DVDFilmotéka exporter";            

            ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("Filmotéka");

            // Hlavicky sloupcu
            Int32 x = 1;
            if (columns.czechName != null) { worksheet.Cell(1, x).Value = "Český název"; x++; }
            if (columns.originalName != null) { worksheet.Cell(1, x).Value = "Originální název"; x++; }

            if (columns.myRating != null) { worksheet.Cell(1, x).Value = "Moje hodnocení"; x++; }
            if (columns.csfdRating != null) { worksheet.Cell(1, x).Value = "CSFD hodnocení"; x++; }
            if (columns.imdbRating != null) { worksheet.Cell(1, x).Value = "IMDB hodnocení"; x++; }

            if (columns.mainGenre != null) { worksheet.Cell(1, x).Value = "Žánr"; x++; }
            if (columns.country != null) { worksheet.Cell(1, x).Value = "Země původu"; x++; }
            if (columns.year != null) { worksheet.Cell(1, x).Value = "Rok"; x++; }
            if (columns.length != null) { worksheet.Cell(1, x).Value = "Délka"; x++; }
            if (columns.directorName != null) { worksheet.Cell(1, x).Value = "Režisér"; x++; }
            if (columns.recenze != null) { worksheet.Cell(1, x).Value = "Recenze"; x++; }

            if (columns.medium != null) { worksheet.Cell(1, x).Value = "Medium"; x++; }
            if (columns.compression != null) { worksheet.Cell(1, x).Value = "Komprese"; x++; }
            if (columns.container != null) { worksheet.Cell(1, x).Value = "Kontejner"; x++; }
            if (columns.quality != null) { worksheet.Cell(1, x).Value = "Kvalita"; x++; }
            if (columns.resolution != null) { worksheet.Cell(1, x).Value = "Rozlišení"; x++; }
            if (columns.size != null) { worksheet.Cell(1, x).Value = "Velikost"; x++; }

            if (columns.language != null) { worksheet.Cell(1, x).Value = "Jazyk"; x++; }
            if (columns.channelCount != null) { worksheet.Cell(1, x).Value = "Počet zvukových kanálů"; x++; }

            if (columns.mediumNotes != null) { worksheet.Cell(1, x).Value = "Poznámky k médiu"; x++; }
            if (columns.movieNotes != null) { worksheet.Cell(1, x).Value = "Poznámky k filmu"; x++; }

            if (columns.link != null) { worksheet.Cell(1, x).Value = "Odkaz"; x++; }

            // Data
            Int32 y = 1;
            foreach (MovieStorage.MovieDataStore12 item in database)
            {
                x = 1;
                y++;
                try
                {
                    if (columns.czechName != null)
                    {
                        if (item.czechName != null) { worksheet.Cell(y, x).Value = item.czechName; x++; } else { worksheet.Cell(y, x).Value = ""; }
                    }
                    
                    if (columns.originalName != null)
                    {
                        if (item.originalName != null) { worksheet.Cell(y, x).Value = item.originalName; x++; } else { worksheet.Cell(y, x).Value = ""; }
                    }

                    if (columns.myRating != null) 
                    {
                        if (item.myRating != null) { worksheet.Cell(y, x).Value = item.myRating; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.csfdRating != null) 
                    {
                        if (item.csfdRating != null) { worksheet.Cell(y, x).Value = item.csfdRating; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.imdbRating != null) 
                    {
                        if (item.imdbRating != null) { worksheet.Cell(y, x).Value = item.imdbRating; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }

                    if (columns.mainGenre != null) 
                    {
                        if (item.mainGenre != null) { worksheet.Cell(y, x).Value = item.mainGenre; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.country != null) 
                    {
                        if (item.country != null) { worksheet.Cell(y, x).Value = item.country; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.year != null) 
                    {
                        if (item.year != null) { worksheet.Cell(y, x).Value = item.year; x++; } else { worksheet.Cell(y, x).Value = ""; }
                    }
                    if (columns.length != null) 
                    {
                        if (item.length != null) { worksheet.Cell(y, x).Value = item.length; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.directorName != null) 
                    {
                        if (item.directorName != null) { worksheet.Cell(y, x).Value = item.directorName; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.recenze != null) 
                    {
                        if (item.recenze != null) { worksheet.Cell(y, x).Value = item.recenze; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }

                    if (columns.medium != null) 
                    {
                        if (item.medium != null) { worksheet.Cell(y, x).Value = item.medium; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.compression != null) 
                    {
                        if (item.compression != null) { worksheet.Cell(y, x).Value = item.compression; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.container != null) 
                    {
                        if (item.container != null) { worksheet.Cell(y, x).Value = item.container; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.quality != null) 
                    {
                        if (item.quality != null) { worksheet.Cell(y, x).Value = item.quality; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.resolution != null) 
                    {
                        if (item.resolution != null) { worksheet.Cell(y, x).Value = item.resolution; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.size != null) 
                    {
                        if (item.size != null) { worksheet.Cell(y, x).Value = item.size; x++; } else { worksheet.Cell(y, x).Value = ""; }
                    }

                    if (columns.language != null) 
                    {
                        if (item.language != null) { worksheet.Cell(y, x).Value = item.language; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.channelCount != null) 
                    {
                        if (item.channelCount != null) { worksheet.Cell(y, x).Value = item.channelCount; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }

                    if (columns.mediumNotes != null) 
                    {
                        if (item.mediumNotes != null) { worksheet.Cell(y, x).Value = item.mediumNotes; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                    if (columns.movieNotes != null) 
                    {
                        if (item.movieNotes != null) { worksheet.Cell(y, x).Value = item.movieNotes; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }

                    if (columns.link != null) 
                    {
                        if (item.link != null) { worksheet.Cell(y, x).Value = item.link; x++; } else { worksheet.Cell(y, x).Value = ""; } 
                    }
                }
                catch 
                {
                    y--;
                }
            }

            xlPackage.Save();
            xlPackage.Dispose();
        }

        #endregion
    }
}
